package com.wokeMy.view.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.commons.math3.util.CombinatoricsUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017-11-15.
 */

public class DoubleGridBallAdapter extends RecyclerView.Adapter<DoubleGridBallAdapter.ViewHolder>{

    public List<String> mDataset;
    private LayoutInflater inflater;
    private Context context;
    TextView zhuTv,moneyTv;

    ArrayList<String> randomBall=new ArrayList<>();
    boolean flag=false;

    boolean colorFlag=false;
    DoubleGridBallAdapter gridBallAdapter;
    int selftBallNum=0,otherBallnum=0; // 外面传进来的
    long zhu=0;
    long money=0;

    public DoubleGridBallAdapter(List<String> dataset, Context context, boolean colorFlag, TextView zhuTv, TextView moneyTv) {
        super();
        mDataset = dataset;
        this.context=context;
        this.colorFlag=colorFlag;
        this.inflater=LayoutInflater.from(context);
        this.randomBall=new ArrayList<>();
        this.selftBallNum=randomBall.size();

        this.zhuTv=zhuTv;
        this.moneyTv=moneyTv;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view =inflater.inflate(R.layout.item_batball,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        selftBallNum=randomBall.size();
        otherBallnum=gridBallAdapter.getChenckBall().size();


        viewHolder.mTextView.setText(mDataset.get(i));
        if (colorFlag){
            viewHolder.mTextView.setTextColor(context.getResources().getColorStateList(R.color.blueMy));
        }
        if (randomBall!=null){
            //            Log.e("onBindViewHolder", randomBall.toString()+"   "+i);
            for (int j = 0; j < randomBall.size(); j++) {
                if (Integer.valueOf(randomBall.get(j))==(i)){
                    flag=true;
                    break;
                }
            }
            if (flag){
                if(colorFlag){
                    //蓝球
                    viewHolder.mTextView.setBackgroundResource(R.mipmap.blue_ball_bg);
                    viewHolder.mTextView.setTextColor(context.getResources().getColorStateList(R.color.white));
                }else{
                    //红球
                    viewHolder.mTextView.setBackgroundResource(R.mipmap.red_ball_bg);
                    viewHolder.mTextView.setTextColor(context.getResources().getColorStateList(R.color.white));
                }

            }else{
                viewHolder.mTextView.setBackgroundResource(R.mipmap.ball);
                if(colorFlag){
                    viewHolder.mTextView.setTextColor(context.getResources().getColorStateList(R.color.blueMy));
                }else{
                    viewHolder.mTextView.setTextColor(context.getResources().getColorStateList(R.color.redMy));
                }

            }

            Log.e("onBindViewHolder",""+this.randomBall +" "+colorFlag);
            Log.e("aaaaaa",otherBallnum+" "+selftBallNum+" "+zhu+" ");
            if(colorFlag){
                //蓝球

                if (selftBallNum<2||otherBallnum<5){
                    zhuTv.setText(0+"注");
                    moneyTv.setText(0+"元");
                }else {
//                    zhu=(int) (Util.combination(otherBallnum,5)*Util.combination(selftBallNum,2));
                    zhu= CombinatoricsUtils.binomialCoefficient(otherBallnum, 5)*CombinatoricsUtils.binomialCoefficient(selftBallNum, 2);
                    money=zhu*2;
                    zhuTv.setText(zhu+"注");
                    moneyTv.setText(money+"元");
                }
            }else{
                //红球

                if (otherBallnum<2||selftBallNum<5){
                    zhuTv.setText(0+"注");
                    moneyTv.setText(0+"元");
                }else {
//                    zhu=(int) (Util.combination(selftBallNum,5)*Util.combination(otherBallnum,2));
                    zhu= CombinatoricsUtils.binomialCoefficient(selftBallNum, 5)*CombinatoricsUtils.binomialCoefficient(otherBallnum, 2);
                    money=zhu*2;
                    Log.e("红球",selftBallNum+" "+otherBallnum+" "+zhu+" ");
                    zhuTv.setText(zhu+"注");
                    moneyTv.setText(money+"元");
                }
            }
            flag=false;


        }

    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView mTextView;

        @Override
        public void onClick(View v) {
            Log.e("getLayoutPosition",getLayoutPosition()+"");
            if(randomBall==null){
                randomBall=new ArrayList<>();
            }

            if (Util.haveNum(getLayoutPosition(),randomBall)){
                for (int i = 0; i < randomBall.size(); i++) {
                    if (Integer.valueOf(randomBall.get(i))==getLayoutPosition()){
                        randomBall.remove(i);
                    }
                }
            }else {
                randomBall.add(getLayoutPosition()+"");
            }
            DoubleGridBallAdapter.this.notifyDataSetChanged();

        }

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.bet_item_tv);
            itemView.setOnClickListener(this);

        }
    }
    public void setRandomBall(ArrayList<String> randomBall){
        this.randomBall.clear();
        this.randomBall=randomBall;
    }
    public void  setGridBallAdapter(DoubleGridBallAdapter gridBallAdapter){
        this.gridBallAdapter=gridBallAdapter;

    }

    public ArrayList<String> getChenckBall(){
        return randomBall;
    }



}
